﻿using System;
using System.Collections.Generic;
using System.Text;
using ExpressionUtil.Common;
using ExpressionUtil.Node.Common;
using ExpressionUtil.Parser;
using System.Diagnostics;
using System.Runtime.Serialization;

namespace ExpressionUtil.Node
{
    /// <summary>
    /// If..Else if..Else条件分支函数节点
    /// </summary>
    [Serializable]
    internal class IfElseIfFunctionNode : Common.FunctionNode, ISerializable
    {
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public IfElseIfFunctionNode()
            : base()
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="info">要填充数据的 System.Runtime.Serialization.SerializationInfo</param>
        /// <param name="context">此序列化的目标</param>
        public IfElseIfFunctionNode(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        /// <summary>
        /// 执行节点运算
        /// </summary>
        /// <param name="context">执行参数</param>
        /// <returns>执行结果的类型</returns>
        public override ExecuteRunTimeType Execute(ExecuteContext context)
        {
            bool hint = false;
            ExecuteRunTimeType type = ExecuteRunTimeType.None;
            //参数弹出式反方向的，所以倒着来......
            for (int i = this.Parameters.Count - 1; i > 0; i -= 2)
            {
                type = this.Parameters[i].Execute(context);
                CheckHelper.IsBoolType(type, "IfElseIf function's condition parameter must be bool type.");
                if (context.GlobalTempBool)
                {
                    type = this.Parameters[i - 1].Execute(context);
                    hint = true;
                    break;
                }
            }
            if (!hint)
            {
                type = this.Parameters[0].Execute(context);
            }
            return type;
        }

        /// <summary>
        /// 创建树节点
        /// </summary>
        /// <param name="context">解析参数</param>
        public static void CreateIfElseIfFunctionNode(ParseContext context)
        {
            int parameterCount = context.FunctionParameterCount.Peek();
            System.Diagnostics.Debug.Assert(parameterCount > 0, "Invalidate parameter count.");
            if (parameterCount < 2 || context.PostfixStack.Count < parameterCount || parameterCount % 2 != 1)
            {
                throw new FormatException("IfElseIf function missing operand.");
            }
            IfElseIfFunctionNode node = new IfElseIfFunctionNode();
            for (; parameterCount > 0; parameterCount--)
            {
                node.Parameters.Add(context.PostfixStack.Pop());
            }
            context.PostfixStack.Push(node);
        }

#if DEBUG
        protected override void DumpCore()
        {
            Debug.WriteLine("IfElseIfFunctionNode,children:");
            for (int i = this.Parameters.Count - 1; i >= 0; i--)
            {
                this.Parameters[i].DumpNode();
            }
        }
#endif
    }
}
